#include <iostream>
#include <pthread.h>
#include <string>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

class Request
{
public:
    Request(int start, int end, const string &threadname)
        : _start(start), _end(end), _threadName(threadname)
    {
    }

public:
    int _start;
    int _end;
    string _threadName;
};

class Response
{
public:
    Response(int result, int exitcode)
        : _result(result), _exitcode(exitcode)
    {
    }

public:
    int _result;   // 计算结果
    int _exitcode; // 计算结果是否可靠
};

void *sumCount(void *args) // 线程的参数和返回值，不仅仅可以用来传递一般参数，也可以传递对象。
{
    Request *rq = static_cast<Request *>(args);
    Response *rsp = new Response(0, 0);
    for (int i = rq->_start; i <= rq->_end; ++i)
    {
        cout << rq->_threadName << " is caling ..." << i << endl;
        rsp->_result += i;
        usleep(100000);
    }
    delete rq;
    return rsp;
}

int main()
{
    pthread_t tid;
    Request *rq = new Request(1, 100, "thread 1");
    pthread_create(&tid, nullptr, sumCount, rq);

    void *ret;
    pthread_join(tid, &ret);
    Response *rsp = static_cast<Response *>(ret);
    cout << "rsp->result: " << rsp->_result << ", rsp->exitcode: " << rsp->_exitcode << endl;
    delete rsp;
    return 0;
}